package hlhj.fhp.tvlib.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.RadioDetailBean
import hlhj.fhp.tvlib.utils.TimeUt
import kotlinx.android.synthetic.main.activity_cast_aty.*
import kotlinx.android.synthetic.main.broad_cast_item.view.*
import java.util.ArrayList

class CastAty : BaseAty() {
    private lateinit var adp:BaseQuickAdapter<RadioDetailBean.DataBean.ProgramBean,BaseViewHolder>
    private lateinit var head:View
    private lateinit var loadingDialog:LoadingDialog
    private val datas=ArrayList<RadioDetailBean.DataBean.ProgramBean>()
    private var id=""
    private var thumb=""
    private var tittle=""
    private var info=""
    private var url=""//音频源
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loadingDialog= LoadingDialog(this)

        id=intent.getIntExtra("id",-1).toString()
        thumb=intent.getStringExtra("thumb")
        tittle=intent.getStringExtra("tittle")
        info=intent.getStringExtra("now")
        tvTittle.text=tittle
        initRcy()
        getData()
        initListener()
    }

    override fun getContentId(): Int {
      return  R.layout.activity_cast_aty
    }

    private fun initRcy() {
        head=LayoutInflater.from(this).inflate(R.layout.broad_cast_item,null)
        Glide.with(this).load(Contents.IMG_URL+thumb).into(head.itemImg)
        head.itemTittle.text=tittle
        if (info.isEmpty()){
            head.itemNow.visibility=View.GONE
        }else{
            head.itemNow.text="正在直播${info}"
        }

        adp=object :BaseQuickAdapter<RadioDetailBean.DataBean.ProgramBean,BaseViewHolder>(R.layout.cast_item,datas){
            override fun convert(helper: BaseViewHolder?, item: RadioDetailBean.DataBean.ProgramBean?) {

            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        adp.addHeaderView(head)

        head.lo.setOnClickListener {
            val intent = Intent(this@CastAty, CastDetailAty::class.java)
            intent.putExtra("url",url)
            intent.putExtra("tittle",tittle)
            intent.putExtra("thumb",thumb)
            startActivity(intent)

        }
    }

    private fun getData() {
        OkGo.get<String>(Contents.BaseUrl+Contents.RADIO_DETAIL)
                .params("program_date",TimeUt.timeStampToStr(System.currentTimeMillis()))
                .params("radio_id",id)
                .execute(object :StringCallback(){
                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                        loadingDialog.show()
                    }
                    override fun onSuccess(response: Response<String>?) {
                        loadingDialog.dismiss()
                        val str = response?.body()
                        val detailBean = Gson().fromJson(str, RadioDetailBean::class.java)
                        if (detailBean.code==1){
                            url=detailBean.data.radio_source
                            datas.addAll(detailBean.data.program)
                            adp.notifyDataSetChanged()
                        }else{
                            Toast.makeText(this@CastAty,detailBean.msg,Toast.LENGTH_SHORT).show()
                        }
                    }
                })
    }

    private fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
    }
}
